import json
import os.path

import pymysql
from PySide6.QtGui import QKeySequence, QShortcut, QAction
from PySide6.QtWidgets import QMainWindow, QMessageBox, QTreeWidgetItem, QTreeWidget, QMenu, QInputDialog, QLineEdit, \
    QPushButton
from PySide6.QtUiTools import QUiLoader
from PySide6.QtCore import Qt, QPoint

from controllers.fav_connection import FavConnection
from global_variables import GlobalVariables
from mysql.model import Model


class TabProxy(QMainWindow):
    def __init__(self, app):
        super().__init__()
        self.app = app
        self.ui = self.app.ui

        self.init()


    def init(self):
        if len(self.ui.proxy_connect_name.text()) > 0:
            self.ui.proxy_add_fav_button.setEnabled(True)

        if len(self.ui.proxy_connect_proxy_host.text()) > 0 \
                and len(self.ui.proxy_connect_username.text()) > 0 \
                and len(self.ui.proxy_connect_password.text()) > 0:
            self.ui.proxy_test_connection_button.setEnabled(True)

        self.define_signals()

    def define_signals(self):
        self.ui.proxy_connection_button.clicked.connect(self.connection_button_clicked)
        self.ui.proxy_test_connection_button.clicked.connect(self.test_connection_button_clicked)
        self.ui.proxy_connect_name.textChanged.connect(self.connect_name_text_changed)
        self.ui.proxy_connect_proxy_port.textChanged.connect(self.connect_port_text_changed)
        self.ui.proxy_connect_port.textChanged.connect(self.connect_port_text_changed)

    def connect_name_text_changed(self, text: str):
        if len(self.ui.proxy_connect_name.text()) > 0:
            self.ui.proxy_add_fav_button.setEnabled(True)
        else:
            self.ui.proxy_add_fav_button.setDisabled(True)

    def connect_port_text_changed(self):
        port = self.sender().text()
        try:
            number = int(port)
        except ValueError:
            self.sender().setText("")
            return

        if number < 1:
            self.sender().setText("3306")
            return
        if number > 65535:
            self.sender().setText("65535")
            return

    def test_connection_button_clicked(self):
        if len(self.ui.proxy_connect_username.text()) == 0:
            return
        proxy_host = self.ui.proxy_connect_proxy_host.text()
        proxy_port = self.ui.proxy_connect_proxy_port.text()

        host, port, user, password = self.get_connection_data()
        try:
            mysqldb = Model(host=host, user=user, password=password, port=port)
            mysqldb.set_app(self.app)
            mysqldb.close()
            QMessageBox.information(self, '操作成功', '连接成功')
        except pymysql.err.OperationalError as e:
            code, msg = e.args
            QMessageBox.critical(self, '错误', msg)

    def get_connection_data(self):
        if len(self.ui.proxy_connect_proxy_host.text()) == 0:
            host = '127.0.0.1'
        else:
            host = self.ui.proxy_connect_proxy_host.text()

        if len(self.ui.proxy_connect_proxy_port.text()) == 0:
            port = 3306
        else:
            port = int(self.ui.proxy_connect_proxy_port.text())

        user = self.ui.proxy_connect_username.text()
        password = self.ui.proxy_connect_password.text()

        return host, port, user, password

    def connection_button_clicked(self):
        host, port, user, password = self.get_connection_data()
        if len(self.ui.proxy_connect_database.text()) == 0:
            database = None
        else:
            database = self.ui.proxy_connect_database.text()

        try:
            mysqldb = Model(host=host, user=user, password=password, port=port)
            mysqldb.set_app(self.app)
        except pymysql.err.OperationalError as e:
            code, msg = e.args
            QMessageBox.critical(self, '错误', msg)
            return
        GlobalVariables.DATABASE_OBJ = mysqldb
        self.app.connection_success(default_database=database)
